package dao

import (
	"dc-news-admin-api/common/utils"
	"dc-news-admin-api/entity"
	"github.com/spf13/cast"
)

func GetUserListByPage(p *utils.Pagination) (users []entity.User, err error) {
	users = make([]entity.User, 0)
	err = DB.Model(&entity.User{}).Scopes(p.GormPaginate()).Find(&users).Error
	if err != nil {
		return
	}
	var count int64
	DB.Model(&entity.User{}).Count(&count)
	p.Total = cast.ToInt(count)
	return
}

func DeleteUserById(id int) (err error) {
	err = DB.
		Model(&entity.User{}).
		Where("id = ?", id).
		Delete(&entity.User{}).
		Error
	return
}

func MuteUserById(id int, isMuted bool) (err error) {
	value := map[string]interface{}{
		"is_muted": isMuted,
	}
	err = DB.
		Model(&entity.User{}).
		Where("id = ?", id).
		Updates(value).
		Error
	return
}
